package com.roin.learn.freemarker.controller;

import com.roin.learn.freemarker.entity.User;
import com.roin.learn.freemarker.service.UserService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;

@Controller
@RequestMapping("/user")  
public class UserController{  
    @Resource  
    private UserService userService;
      
    @RequestMapping("/getById.htm")    
    public ModelAndView getById(@RequestParam("id")String userId){      
        ModelAndView mav = new ModelAndView("user/user");
        User user = userService.selectUserById(userId);
        mav.addObject("user", user);   
        return mav;    
    } 
    @RequestMapping("/getById.do")
    public ModelAndView getByIdJsp(@RequestParam("id")String userId){
        ModelAndView mav = new ModelAndView("/user/user.jsp");
        User user = userService.selectUserById(userId);
        mav.addObject("user", user);
        return mav;
    }

    @RequestMapping("/updateById.do")
    @ResponseBody
    public String getUserById(User user){      
    	if (user!=null) {
    		int row = userService.update(user);
    		return row>0?"修改成功！":"修改失败!";
		}else{
			return "修改失败!user参数为空！";
		}
    }  
}
